# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jaewon

from enum import IntEnum


class OrderGatewayStatus(IntEnum):
  UNKNOWN = 0
  INITIALIZING = 1
  READY = 2
  MARKET_CLOSED = 3
  DISCONNECTED = 4
  REINITIALIZING = 5


class OrderGatewayReady(object):
  def __init__(self):
    self.reset()

  @property
  def status(self) -> OrderGatewayStatus:
    return self._status

  @property
  def ready(self):
    return self._status == OrderGatewayStatus.READY

  def is_ready(self):
    return self._status == OrderGatewayStatus.READY

  def set_status(self, status):
    self._status = status

  def set_init(self):
    self.reset()
    self._status = OrderGatewayStatus.INITIALIZING

  def set_reinit(self):
    self.reset()
    self._status = OrderGatewayStatus.REINITIALIZING

  def reset(self):
    self._status = OrderGatewayStatus.UNKNOWN

  def check_ready(self):
    raise NotImplementedError()


class SpotOrderGatewayReady(OrderGatewayReady):
  def reset(self):
    self._status = OrderGatewayStatus.UNKNOWN
    self._balance_ready = False
    self._orders_ready = False

  @property
  def balance_ready(self):
    return self._balance_ready

  @property
  def orders_ready(self):
    return self._orders_ready

  def set_balance_ready(self, ready):
    self._balance_ready = ready

  def set_orders_ready(self, ready):
    self._orders_ready = ready

  def check_ready(self):
    if (self._balance_ready and self._orders_ready):
      self.set_status(OrderGatewayStatus.READY)


class PositionOrderGatewayReady(OrderGatewayReady):
  def reset(self):
    self._status = OrderGatewayStatus.UNKNOWN
    self._balance_ready = False
    self._position_ready = False
    self._orders_ready = False

  @property
  def balance_ready(self):
    return self._balance_ready

  @property
  def position_ready(self):
    return self._position_ready

  @property
  def orders_ready(self):
    return self._orders_ready

  def set_balance_ready(self, ready):
    self._balance_ready = ready

  def set_position_ready(self, ready):
    self._position_ready = ready

  def set_orders_ready(self, ready):
    self._orders_ready = ready

  def check_ready(self):
    if (self._balance_ready and self._position_ready and self._orders_ready):
      self.set_status(OrderGatewayStatus.READY)


class SpotLPOrderGatewayReady(OrderGatewayReady):
  def reset(self):
    self._status = OrderGatewayStatus.UNKNOWN
    self._balance_ready = False
    self._orders_ready = False
    self._nft_token_info_ready = False

  @property
  def balance_ready(self):
    return self._balance_ready

  @property
  def orders_ready(self):
    return self._orders_ready

  @property
  def nft_token_info_ready(self):
    return self._nft_token_info_ready

  def set_balance_ready(self, ready):
    self._balance_ready = ready

  def set_orders_ready(self, ready):
    self._orders_ready = ready

  def set_nft_token_info_ready(self, ready):
    self._nft_token_info_ready = ready

  def check_ready(self):
    if self._balance_ready and self._orders_ready and self._nft_token_info_ready:
      self.set_status(OrderGatewayStatus.READY)
